﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Transactions;
using System.Data.SqlClient;

public partial class Main_Item_NewItemEdit : System.Web.UI.Page
{

    //For the Local variable and the List
    #region
    Datautility dut = new Datautility();
    CommonLog ObjLog = new CommonLog();
    string ITMID = "";
    string strsql = "";
    int ControlCounter = 4;
    int count = 1;
    int mpnCounter = 3;   
    List<string> LstID;
    List<string> LstMPNNo;
    List<string> LstMPNName;
    List<string> LstMFRLTime;
    List<string> LstTransitLTime;
    List<string> LstTotalLTime;
    List<string> LstD1;
    List<string> LstD2;
    List<string> LstD3;    
    string UserId = "";
    string Username = "";
    string UpdateMPNLogDetail = ""; string UpdateItemOtherLogDetail = "";
    string UpdateItemAlternateUOMLog = ""; string UpdateOpeningBalanceLog = "";
    string UpdateItemMasterlog = "";
    #endregion
    //For the AddNew Row Concept on the Page
    #region
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        LstID = (List<string>)ViewState["LstID"];
        LstMPNNo = (List<string>)ViewState["LstMPNNo"];
        LstMPNName = (List<string>)ViewState["LstMPNName"];
        LstMFRLTime = (List<string>)ViewState["LstMFRLTime"];
        LstD1 = (List<string>)ViewState["LstD1"];
        LstTransitLTime = (List<string>)ViewState["LstTransitLTime"];
        LstD2 = (List<string>)ViewState["LstD2"];
        LstTotalLTime = (List<string>)ViewState["LstTotalLTime"];
        LstD3 = (List<string>)ViewState["LstD3"];
        if (LstID != null)
        {
            int count = Convert.ToInt32(LstID.Count.ToString());
            for (int i = 0; i < count; i++)
            {
                mpnCounter++;
                Label lbl = new Label();
                lbl.ID = LstID[i].ToString();
                lbl.Text = mpnCounter.ToString();

                HtmlTableRow htrow = new HtmlTableRow();
                htrow.ID = "htrow" + mpnCounter.ToString();

                HtmlTableCell htcell1 = new HtmlTableCell();
                htcell1.ID = "htcell1" + mpnCounter.ToString();
                TextBox txtmanufacturecode = new TextBox();
                txtmanufacturecode.ID = LstMPNNo[i].ToString();
                txtmanufacturecode.Width = Unit.Percentage(71);
                htcell1.Controls.Add(txtmanufacturecode);

                HtmlTableCell htcell2 = new HtmlTableCell();
                htcell2.ID = "htcell2" + mpnCounter.ToString();
                DropDownList ddlManufacturer = new DropDownList();
                ddlManufacturer.ID = LstMPNName[i].ToString();
                ddlManufacturer.Width = Unit.Percentage(73);
                ddlManufacturer.CssClass = "dropdown";
                htcell2.Controls.Add(ddlManufacturer);

                HtmlTableCell htcell3 = new HtmlTableCell();
                htcell3.ID = "htcell3" + mpnCounter.ToString();
                TextBox txtmfrleadtime = new TextBox();
                txtmfrleadtime.ID = LstMFRLTime[i].ToString();
                txtmfrleadtime.Width = Unit.Percentage(71);
                txtmfrleadtime.Attributes.Add("onblur", "calcLeadTime();");
                Label lblleadtime = new Label();
                lblleadtime.ID = LstD1[i].ToString();
                lblleadtime.Text = "(D)";

                htcell3.Controls.Add(txtmfrleadtime);
                htcell3.Controls.Add(lblleadtime);

                HtmlTableCell htcell4 = new HtmlTableCell();
                htcell4.ID = "htcell4" + mpnCounter.ToString();
                TextBox txttransitleadtime = new TextBox();
                txttransitleadtime.ID = LstTransitLTime[i].ToString();
                txttransitleadtime.Width = Unit.Percentage(72);
                txttransitleadtime.Attributes.Add("onblur", "calcLeadTime();");
                Label lblTransittotal = new Label();
                lblTransittotal.ID = LstD2[i].ToString();
                lblTransittotal.Text = "(D)";
                htcell4.Controls.Add(txttransitleadtime);
                htcell4.Controls.Add(lblTransittotal);

                HtmlTableCell htcell5 = new HtmlTableCell();
                htcell5.ID = "htcell5" + mpnCounter.ToString();
                TextBox txttotalleadtime = new TextBox();
                txttotalleadtime.ID = LstTotalLTime[i].ToString();
                txttotalleadtime.Width = Unit.Percentage(76);
                txttotalleadtime.ReadOnly = true;
                txttotalleadtime.Attributes.Add("onkeypress", "return isNumberKey(this.id,event)");
                txttotalleadtime.CssClass = "input";
                txttotalleadtime.Style["background-color"] = "LightGray";
                Label lbltotal = new Label();
                lbltotal.ID = LstD3[i].ToString();
                lbltotal.Text = "(D)";
                htcell5.Controls.Add(txttotalleadtime);
                htcell5.Controls.Add(lbltotal);

                htrow.Cells.Add(htcell1);
                htrow.Cells.Add(htcell2);
                htrow.Cells.Add(htcell3);
                htrow.Cells.Add(htcell4);
                htrow.Cells.Add(htcell5);
                tblMPN.Rows.Add(htrow);
            }
        }
        #region Comment
        //base.LoadViewState(savedState);
        //LstList = (List<string>)ViewState["LstList"];
        //LstLocaion = (List<string>)ViewState["LstLocaion"];
        //LstBatch = (List<string>)ViewState["LstBatch"];
        //LstVendor = (List<string>)ViewState["LstVendor"];
        //LstRefNo = (List<string>)ViewState["LstRefNo"];
        //LstMfg = (List<string>)ViewState["LstMfg"];

        //LstMfgCal = (List<string>)ViewState["LstMfgCal"];
        //LstMfgMask = (List<string>)ViewState["LstMfgMask"];
        //LstMfgMaskVal = (List<string>)ViewState["LstMfgMaskVal"];
        //LstExpire = (List<string>)ViewState["LstExpire"];
        //LstExpireCal = (List<string>)ViewState["LstExpireCal"];
        //LstExpireMask = (List<string>)ViewState["LstExpireMask"];
        //LstExpireMaskVal = (List<string>)ViewState["LstExpireMaskVal"];

        //LstQty = (List<string>)ViewState["LstQty"];
        //LstRate = (List<string>)ViewState["LstRate"];
        //LstValue = (List<string>)ViewState["LstValue"];
        //LstWarranty = (List<string>)ViewState["LstWarranty"];
        //LstWarrantyDetails = (List<string>)ViewState["LstWarrantyDetails"];
        //LstRemarks = (List<string>)ViewState["LstRemarks"];


        //LstBat = (List<string>)ViewState["LstBat"];
        //LstLoc = (List<string>)ViewState["LstLoc"];

        //if (LstList != null)
        //{
        //    int count = Convert.ToInt32(LstList.Count.ToString());

        //    for (int i = 0; i < count; i++)
        //    {
        //        ControlCounter++;

        //        Label lbl = new Label();
        //        lbl.ID = LstList[i].ToString();
        //        lbl.Text = ControlCounter.ToString();

        //        HtmlTableRow htrow = new HtmlTableRow();
        //        htrow.ID = "htrow" + ControlCounter.ToString();

        //        HtmlTableCell htcell1 = new HtmlTableCell();
        //        htcell1.VAlign = "Top";
        //        DropDownList dropdown = new DropDownList();
        //        dropdown.ID = LstLocaion[i].ToString();
        //        dropdown.AutoPostBack = true;
        //        dropdown.CssClass = "dropdown";
        //        HiddenField hidloc = new HiddenField();
        //        hidloc.ID = LstLoc[i].ToString();
        //        htcell1.Controls.Add(dropdown);
        //        htcell1.Controls.Add(hidloc);


        //        HtmlTableCell htcell2 = new HtmlTableCell();
        //        htcell2.VAlign = "Top";
        //        TextBox TxtBatch = new TextBox();
        //        TxtBatch.ID = LstBatch[i].ToString();
        //        TxtBatch.AutoPostBack = true;
        //        TxtBatch.Width = Unit.Pixel(50);
        //        TxtBatch.CssClass = "input";
        //        TxtBatch.AutoCompleteType = AutoCompleteType.Disabled;
        //        HiddenField hidbat = new HiddenField();
        //        hidbat.ID = LstBat[i].ToString();
        //        htcell2.Controls.Add(TxtBatch);
        //        htcell2.Controls.Add(hidbat);

        //        HtmlTableCell htcell3 = new HtmlTableCell();
        //        htcell3.VAlign = "Top";
        //        DropDownList Ddlvendor = new DropDownList();
        //        Ddlvendor.ID = LstVendor[i].ToString();
        //        Ddlvendor.AutoPostBack = true;
        //        Ddlvendor.CssClass = "dropdown";
        //        htcell3.Controls.Add(Ddlvendor);

        //        HtmlTableCell htcell4 = new HtmlTableCell();
        //        htcell4.VAlign = "Top";
        //        TextBox TxtRefno = new TextBox();
        //        TxtRefno.ID = LstRefNo[i].ToString();
        //        TxtRefno.AutoPostBack = true;
        //        TxtRefno.Width = Unit.Pixel(50);
        //        TxtRefno.CssClass = "input";
        //        TxtRefno.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell4.Controls.Add(TxtRefno);

        //        HtmlTableCell htcell5 = new HtmlTableCell();
        //        htcell5.VAlign = "Top";
        //        TextBox TxtMfgDate = new TextBox();
        //        TxtMfgDate.ID = LstMfg[i].ToString();
        //        TxtMfgDate.Width = Unit.Pixel(50);
        //        TxtMfgDate.CssClass = "input";
        //        TxtMfgDate.AutoCompleteType = AutoCompleteType.Disabled;
        //        AjaxControlToolkit.CalendarExtender CalMfd = new AjaxControlToolkit.CalendarExtender();
        //        CalMfd.ID = LstMfgCal[i].ToString();
        //        CalMfd.Format = "dd/MM/yyyy";
        //        CalMfd.TargetControlID = TxtMfgDate.ID;
        //        AjaxControlToolkit.MaskedEditExtender MaskMfd = new AjaxControlToolkit.MaskedEditExtender();
        //        MaskMfd.ID = LstMfgMask[i].ToString();
        //        MaskMfd.Mask = "99/99/9999";
        //        MaskMfd.TargetControlID = TxtMfgDate.ID;
        //        MaskMfd.CultureName = "en-GB";
        //        MaskMfd.MaskType = AjaxControlToolkit.MaskedEditType.Date;
        //        AjaxControlToolkit.MaskedEditValidator MaskEditVal = new AjaxControlToolkit.MaskedEditValidator();
        //        MaskEditVal.ID = LstMfgMaskVal[i].ToString();
        //        MaskEditVal.ControlExtender = MaskMfd.ID;
        //        MaskEditVal.ControlToValidate = TxtMfgDate.ID;
        //        MaskEditVal.Display = ValidatorDisplay.Dynamic;
        //        MaskEditVal.ErrorMessage = "*";
        //        htcell5.Controls.Add(TxtMfgDate);
        //        htcell5.Controls.Add(CalMfd);
        //        htcell5.Controls.Add(MaskMfd);
        //        htcell5.Controls.Add(MaskEditVal);


        //        HtmlTableCell htcell6 = new HtmlTableCell();
        //        htcell6.VAlign = "Top";
        //        TextBox TxtExpireDate = new TextBox();
        //        TxtExpireDate.ID = LstExpire[i].ToString();
        //        TxtExpireDate.Width = Unit.Pixel(50);
        //        TxtExpireDate.CssClass = "input";
        //        TxtExpireDate.AutoCompleteType = AutoCompleteType.Disabled;
        //        AjaxControlToolkit.CalendarExtender CalExpiry = new AjaxControlToolkit.CalendarExtender();
        //        CalExpiry.ID = LstExpireCal[i].ToString();
        //        CalExpiry.Format = "dd/MM/yyyy";
        //        CalExpiry.TargetControlID = TxtExpireDate.ID;
        //        AjaxControlToolkit.MaskedEditExtender MaskExpire = new AjaxControlToolkit.MaskedEditExtender();
        //        MaskExpire.ID = LstExpireMask[i].ToString();
        //        MaskExpire.Mask = "99/99/9999";
        //        MaskExpire.TargetControlID = TxtExpireDate.ID;
        //        MaskExpire.CultureName = "en-GB";
        //        MaskExpire.MaskType = AjaxControlToolkit.MaskedEditType.Date;
        //        AjaxControlToolkit.MaskedEditValidator MaskEditValExpire = new AjaxControlToolkit.MaskedEditValidator();
        //        MaskEditValExpire.ID = LstExpireMaskVal[i].ToString();
        //        MaskEditValExpire.ControlExtender = MaskExpire.ID;
        //        MaskEditValExpire.ControlToValidate = TxtExpireDate.ID;
        //        MaskEditValExpire.Display = ValidatorDisplay.Dynamic;
        //        MaskEditValExpire.ErrorMessage = "*";
        //        htcell6.Controls.Add(TxtExpireDate);
        //        htcell6.Controls.Add(CalExpiry);
        //        htcell6.Controls.Add(MaskExpire);
        //        htcell6.Controls.Add(MaskEditValExpire);

        //        HtmlTableCell htcell7 = new HtmlTableCell();
        //        htcell7.VAlign = "Top";
        //        TextBox TxtQty = new TextBox();
        //        TxtQty.ID = LstQty[i].ToString();
        //        TxtQty.Width = Unit.Pixel(50);
        //        TxtQty.CssClass = "inputnumber";
        //        TxtQty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        //        TxtQty.Attributes.Add("onblur", "calcQty();");
        //        TxtQty.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell7.Controls.Add(TxtQty);

        //        HtmlTableCell htcell8 = new HtmlTableCell();
        //        htcell8.VAlign = "Top";
        //        TextBox TxtRate = new TextBox();
        //        TxtRate.ID = LstRate[i].ToString();
        //        TxtRate.Width = Unit.Pixel(50);
        //        TxtRate.CssClass = "inputnumber";
        //        TxtRate.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        //        TxtRate.Attributes.Add("onblur", "calcQty();");
        //        TxtRate.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell8.Controls.Add(TxtRate);


        //        HtmlTableCell htcell9 = new HtmlTableCell();
        //        htcell9.VAlign = "Top";
        //        TextBox TxtValue = new TextBox();
        //        TxtValue.ID = LstValue[i].ToString();
        //        TxtValue.Width = Unit.Pixel(50);
        //        TxtValue.CssClass = "inputnumber";
        //        TxtValue.Enabled = false;
        //        TxtValue.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        //        TxtValue.Attributes.Add("onblur", "calcQty();");
        //        TxtValue.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell9.Controls.Add(TxtValue);

        //        HtmlTableCell htcell10 = new HtmlTableCell();
        //        htcell10.VAlign = "Top";
        //        TextBox TxtWarranty = new TextBox();
        //        TxtWarranty.ID = LstWarranty[i].ToString();
        //        TxtWarranty.Width = Unit.Pixel(50);
        //        TxtWarranty.CssClass = "inputnumber";
        //        TxtWarranty.Attributes.Add("onKeyPress", "return isNumberKey(this.id,event);");
        //        TxtWarranty.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell10.Controls.Add(TxtWarranty);

        //        HtmlTableCell htcell11 = new HtmlTableCell();
        //        htcell11.VAlign = "Top";
        //        DropDownList dropWarranty = new DropDownList();
        //        dropWarranty.ID = LstWarrantyDetails[i].ToString();
        //        dropWarranty.AutoPostBack = true;
        //        dropWarranty.CssClass = "dropdown";
        //        htcell11.Controls.Add(dropWarranty);


        //        HtmlTableCell htcell12 = new HtmlTableCell();
        //        htcell12.VAlign = "Top";
        //        TextBox TxtRemark = new TextBox();
        //        TxtRemark.ID = LstRemarks[i].ToString();
        //        TxtRemark.Width = Unit.Pixel(50);
        //        TxtRemark.CssClass = "input";
        //        TxtRemark.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell12.Controls.Add(TxtRemark);



        //        htrow.Cells.Add(htcell1);
        //        htrow.Cells.Add(htcell2);
        //        htrow.Cells.Add(htcell3);
        //        htrow.Cells.Add(htcell4);
        //        htrow.Cells.Add(htcell5);
        //        htrow.Cells.Add(htcell6);
        //        htrow.Cells.Add(htcell7);
        //        htrow.Cells.Add(htcell8);
        //        htrow.Cells.Add(htcell9);
        //        htrow.Cells.Add(htcell10);
        //        htrow.Cells.Add(htcell11);
        //        htrow.Cells.Add(htcell12);
        //        tbl.Rows.Add(htrow);
        //    }
        //}
        //lstlistsecond = (List<string>)ViewState["lstlistsecond"];
        //lstlocationsecond = (List<string>)ViewState["lstlocationsecond"];
        //lstqtysecond = (List<string>)ViewState["lstqtysecond"];
        //lstratesecond = (List<string>)ViewState["lstratesecond"];
        //lstvaluesecond = (List<string>)ViewState["lstvaluesecond"];
        //lstopeningdate = (List<string>)ViewState["lstopeningdate"];
        //if (lstlistsecond != null)
        //{
        //    int countsecond = Convert.ToInt32(lstlistsecond.Count.ToString());
        //    for (int j = 0; j < countsecond; j++)
        //    {
        //        count++;

        //        Label lbl = new Label();
        //        lbl.ID = lstlistsecond[j].ToString();
        //        lbl.Text = count.ToString();

        //        HtmlTableRow htrow = new HtmlTableRow();
        //        htrow.ID = "htrowsecond" + count.ToString();
        //        HtmlTableCell htcell1 = new HtmlTableCell();
        //        htcell1.ID = "htcell1" + count.ToString();
        //        htcell1.VAlign = "Top";
        //        //htcell1.Align = "center";
        //        DropDownList ddlloc = new DropDownList();
        //        ddlloc.ID = lstlocationsecond[j].ToString();
        //        ddlloc.AutoPostBack = true;
        //        ddlloc.Width = Unit.Pixel(150);
        //        ddlloc.CssClass = "dropdown";
        //        htcell1.Controls.Add(ddlloc);
        //        //BindDdlLocwithdisbatch();

        //        DataTable dt = dut.getdata("select WHSLOCATION, WHSID from IVR_WAREHOUSE");
        //        ddlloc.DataSource = dt;
        //        ddlloc.DataTextField = "WHSLOCATION";
        //        ddlloc.DataValueField = "WHSID";
        //        ddlloc.DataBind();


        //        HtmlTableCell htcell2 = new HtmlTableCell();
        //        htcell2.ID = "htcell2" + count.ToString();
        //        htcell2.VAlign = "Top";
        //        //htcell2.Align = "center";
        //        TextBox txtqtymain = new TextBox();
        //        txtqtymain.ID = lstqtysecond[j].ToString();
        //        txtqtymain.Width = Unit.Pixel(140);
        //        txtqtymain.CssClass = "input";
        //        //txtqtymain.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell2.Controls.Add(txtqtymain);

        //        HtmlTableCell htcell3 = new HtmlTableCell();
        //        htcell3.ID = "htcell3" + count.ToString();
        //        htcell3.VAlign = "Top";
        //        //htcell3.Align = "center";
        //        TextBox txtratemain = new TextBox();
        //        txtratemain.ID = lstratesecond[j].ToString();
        //        //txtratemain.AutoPostBack = true;
        //        txtratemain.Width = Unit.Pixel(140);
        //        txtratemain.CssClass = "input";
        //        txtratemain.Attributes.Add("onblur", "return CalculateAmount();");
        //        //txtqtymain.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell3.Controls.Add(txtratemain);

        //        HtmlTableCell htcell4 = new HtmlTableCell();
        //        htcell4.ID = "htcell4" + count.ToString();
        //        htcell4.VAlign = "Top";
        //        //htcell4.Align = "center";
        //        TextBox txttotalvalue = new TextBox();
        //        txttotalvalue.ID = lstvaluesecond[j].ToString();
        //        //txttotalvalue.AutoPostBack = true;
        //        txttotalvalue.Width = Unit.Pixel(140);
        //        txttotalvalue.CssClass = "input";
        //        //txtqtymain.AutoCompleteType = AutoCompleteType.Disabled;
        //        htcell4.Controls.Add(txttotalvalue);

        //        HtmlTableCell htcell5 = new HtmlTableCell();
        //        htcell5.ID = "htcell5" + count.ToString();
        //        htcell5.VAlign = "Top";
        //        TextBox txtopeningdate = new TextBox();
        //        txtopeningdate.ID = lstopeningdate[j].ToString();
        //        txtopeningdate.CssClass = "input";
        //        txtopeningdate.Width = Unit.Pixel(100);
        //        txtopeningdate.Enabled = false;
        //        txtopeningdate.Text = "2013-04-01";
        //        htcell5.Controls.Add(txtopeningdate);
        //        //lstopeningdate.Add(txtopeningdate.ID);

        //        htrow.Cells.Add(htcell5);
        //        htrow.Cells.Add(htcell1);
        //        htrow.Cells.Add(htcell2);
        //        htrow.Cells.Add(htcell3);
        //        htrow.Cells.Add(htcell4);

        //        tbldisbatch.Rows.Add(htrow);
        //    }
        //}
        #endregion
    }
    protected void CreateNewRow()
    {
        mpnCounter++;
        Label lbl = new Label();
        lbl.ID = "lbl" + mpnCounter.ToString();
        lbl.Text = mpnCounter.ToString();
        LstID.Add(lbl.Text);

        HtmlTableRow htrow = new HtmlTableRow();
        htrow.ID = "htrow" + mpnCounter.ToString();

        HtmlTableCell htcell1 = new HtmlTableCell();
        htcell1.ID = "htcell1" + mpnCounter.ToString();
        TextBox txtmanufacturecode = new TextBox();
        txtmanufacturecode.ID = "txtmanufacturecode" + mpnCounter.ToString();
        txtmanufacturecode.Width = Unit.Percentage(71);
        htcell1.Controls.Add(txtmanufacturecode);
        LstMPNNo.Add(txtmanufacturecode.ID);

        HtmlTableCell htcell2 = new HtmlTableCell();
        htcell2.ID = "htcell2" + mpnCounter.ToString();
        DropDownList ddlManufacturer = new DropDownList();
        ddlManufacturer.ID = "ddlManufacturer" + mpnCounter.ToString();
        ddlManufacturer.Width = Unit.Percentage(73);
        ddlManufacturer.CssClass = "dropdown";
        htcell2.Controls.Add(ddlManufacturer);
        LstMPNName.Add(ddlManufacturer.ID);

        HtmlTableCell htcell3 = new HtmlTableCell();
        htcell3.ID = "htcell3" + mpnCounter.ToString();
        TextBox txtmfrleadtime = new TextBox();
        txtmfrleadtime.ID = "txtmfrleadtime" + mpnCounter.ToString();
        txtmfrleadtime.Width = Unit.Percentage(71);
        txtmfrleadtime.Attributes.Add("onblur", "calcLeadTime();");
        htcell3.Controls.Add(txtmfrleadtime);

        Label lblleadtime = new Label();
        lblleadtime.ID = "lblleadtime" + mpnCounter.ToString();
        lblleadtime.Text = "(D)";
        htcell3.Controls.Add(lblleadtime);

        LstMFRLTime.Add(txtmfrleadtime.ID);
        LstD1.Add(lblleadtime.ID);

        HtmlTableCell htcell4 = new HtmlTableCell();
        htcell4.ID = "htcell4" + mpnCounter.ToString();
        TextBox txttransitleadtime = new TextBox();
        txttransitleadtime.ID = "txttransitleadtime" + mpnCounter.ToString();
        txttransitleadtime.Width = Unit.Percentage(72);
        txttransitleadtime.Attributes.Add("onblur", "calcLeadTime();");
        htcell4.Controls.Add(txttransitleadtime);

        Label lblTransittotal = new Label();
        lblTransittotal.ID = "lblTransittotal" + mpnCounter.ToString();
        lblTransittotal.Text = "(D)";
        htcell4.Controls.Add(lblTransittotal);

        LstTransitLTime.Add(txttransitleadtime.ID);
        LstD2.Add(lblTransittotal.ID);

        HtmlTableCell htcell5 = new HtmlTableCell();
        htcell5.ID = "htcell5" + mpnCounter.ToString();
        TextBox txttotalleadtime = new TextBox();
        txttotalleadtime.ID = "txttotalleadtime" + mpnCounter.ToString();
        txttotalleadtime.Width = Unit.Percentage(76);
        txttotalleadtime.ReadOnly = true;
        txttotalleadtime.Attributes.Add("onkeypress", "return isNumberKey(this.id,event)");
        txttotalleadtime.CssClass = "input";
        txttotalleadtime.Style["background-color"] = "LightGray";
        htcell5.Controls.Add(txttotalleadtime);

        Label lbltotal = new Label();
        lbltotal.ID = "lbltotal" + mpnCounter.ToString();
        lbltotal.Text = "(D)";
        htcell5.Controls.Add(lbltotal);
        LstTotalLTime.Add(txttotalleadtime.ID);
        LstD3.Add(lbltotal.ID);

        htrow.Cells.Add(htcell1);
        htrow.Cells.Add(htcell2);
        htrow.Cells.Add(htcell3);
        htrow.Cells.Add(htcell4);
        htrow.Cells.Add(htcell5);
        tblMPN.Rows.Add(htrow);

        ViewState["LstID"] = LstID;
        ViewState["LstMPNNo"] = LstMPNNo;
        ViewState["LstMPNName"] = LstMPNName;
        ViewState["LstMFRLTime"] = LstMFRLTime;
        ViewState["LstD1"] = LstD1;
        ViewState["LstTransitLTime"] = LstTransitLTime;
        ViewState["LstD2"] = LstD2;
        ViewState["LstTotalLTime"] = LstTotalLTime;
        ViewState["LstD3"] = LstD3;
        //HidMPNControl.Value = mpnCounter.ToString();
        GetManufacturer();
    }
    #endregion
    //For the Page Load
    #region
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        else
        {           
             //For Checking the Permission of the Page according to the User.
            string Pagepermission = "select PAGE_ID,PAGE_EDIT,PAGE_VIEW,USERID,USERNAME as EmailID from USER_PERMISSION inner join LOGIN on USER_PERMISSION.USER_ID=LOGIN.USERID where USER_ID='" + Session["userid"].ToString() + "' and PAGE_ID=407";
            DataTable dt = dut.getdata(Pagepermission);
            if (dt.Rows.Count > 0)
            {
                if (Request.QueryString["ITMID"] != null)
                {
                    if (dt.Rows[0]["PAGE_EDIT"].ToString() == "True")
                    {
                        ITMID = Request.QueryString["ITMID"].ToString();
                        if (!IsPostBack)
                        {
                            #region
                            LstID = new List<string>();
                            ViewState["LstID"] = LstID;
                            LstMPNNo = new List<string>();
                            ViewState["LstMPNNo"] = LstMPNNo;
                            LstMPNName = new List<string>();
                            ViewState["LstMPNName"] = LstMPNName;
                            LstMFRLTime = new List<string>();
                            ViewState["LstMFRLTime"] = LstMFRLTime;
                            LstD1 = new List<string>();
                            ViewState["LstD1"] = LstD1;
                            LstTransitLTime = new List<string>();
                            ViewState["LstTransitLTime"] = LstTransitLTime;
                            LstD2 = new List<string>();
                            ViewState["LstD2"] = LstD2;
                            LstTotalLTime = new List<string>();
                            ViewState["LstTotalLTime"] = LstTotalLTime;
                            LstD3 = new List<string>();
                            ViewState["LstD3"] = LstD3;
                            //EnableBatch();
                            openingdate();
                            Itemsettings();
                            BindItemGroup();
                            BindCategory();
                            BindPrimaryUnit();
                            BindStock();
                            GetProject();
                            GetManufacturer();
                            bindEditForm(ITMID);
                            #endregion
                            //TxtItemCode.Enabled = false;
                            UserId = Session["userid"].ToString();
                            Username = Session["username"].ToString();
                        }
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }                
            }
        }        
    }
    private void bindEditForm(string ITMID)
    {
        string IP = Request.ServerVariables["REMOTE_ADDR"];
        UserId = Session["userid"].ToString();
        Username = Session["username"].ToString();
        string MPNLogDetail = ""; string ItemOtherLogDetail = "";
        string ItemAlternateUOMLog = ""; string OpeningBalanceLog = "";
        string ItemMasterlog = "";
        DataTable DT = dut.getdata("select * from IVR_ITEM where ITMID=" + ITMID);
        if (DT.Rows.Count > 0)
        {
            GetLocation();
            //Item Master
            #region ItemMaster
            TxtItemCode.Text = DT.Rows[0]["ITMCODE"].ToString();
            TxtItemCode.Enabled = false;
            TxtItemDescription.Text = DT.Rows[0]["ITMDESC"].ToString();
            TxtItemDescription.Enabled = false;
            TxtAlternateDesc.Text = DT.Rows[0]["ITMALTDESC"].ToString();
            TxtAlternateDesc.Enabled = false;
            TxtAdditionalDesc.Text = DT.Rows[0]["ITMADDDESC"].ToString();
            TxtReorder.Text = Convert.ToDouble(DT.Rows[0]["REORDERQTY"].ToString()).ToString("N2");
            TxtMinimum.Text = Convert.ToDouble(DT.Rows[0]["MINQTY"].ToString()).ToString("N2");
            TxtMaximum.Text = Convert.ToDouble(DT.Rows[0]["MAXQTY"].ToString()).ToString("N2");
            ddlproject.SelectedValue = DT.Rows[0]["Projectid"].ToString();
            hidTotalOp.Value = DT.Rows[0]["OpeningStock"].ToString();
            txtlocationid.Text = DT.Rows[0]["locationID"].ToString();
            string selected_ItemGroup = DT.Rows[0]["IGRID"].ToString();
            DDLItemGroup.SelectedValue = selected_ItemGroup;
            string selected_Category = DT.Rows[0]["ICAID"].ToString();
            DDLCategory.SelectedValue = selected_Category;
            string selected_primaryunit = DT.Rows[0]["PRIMARY_UOMID"].ToString();
            DDLPrimaryUnit.SelectedValue = selected_primaryunit;
            string selected_StockType = DT.Rows[0]["StockTypeId"].ToString();
            DDLStockType.SelectedValue = selected_StockType;

            //For Log
            ItemMasterlog = "Item Code:" + TxtItemCode.Text + ",ItemDesc:" + TxtItemDescription.Text + ",AlternateDesc:" + TxtAlternateDesc.Text + ",AdditionalDesc:" + TxtAdditionalDesc.Text + ",ItemGroup:" + DDLItemGroup.SelectedItem + ",ItemCategory:" + DDLCategory.SelectedItem + ",StockType:" + DDLStockType.SelectedItem + ",PrimaryUnit:" + DDLPrimaryUnit.SelectedItem + ",Maximum:" + TxtMaximum.Text + ",Minimum:" + TxtMinimum.Text + ",Reorder:" + TxtReorder.Text + ",OpeningStock" + hidTotalOp.Value + ",Project:" + ddlproject.SelectedItem;
            #endregion
            //for MPN
            #region ItemMPN
            string SqlMpn = "Select * from IVR_ITEM_MPN where ITEMID='" + ITMID + "'";
            DataTable DtMpn = dut.getdata(SqlMpn);
            if (DtMpn.Rows.Count > 0)
            {
                if (DtMpn.Rows.Count > 3)
                {
                    for (int p = 4; p <= DtMpn.Rows.Count; p++)
                    {
                        CreateNewRow();
                    }
                }
                for (int m = 1; m <= DtMpn.Rows.Count; m++)
                {
                    TextBox txtMPNPartNo = (TextBox)tblMPN.Rows[m - 1].FindControl("txtmanufacturecode" + m.ToString());
                    DropDownList ddlMPNName = (DropDownList)tblMPN.Rows[m - 1].FindControl("ddlManufacturer" + m.ToString());
                    TextBox txtMfrLeadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txtmfrleadtime" + m.ToString());
                    TextBox txttransiteLeadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txttransitleadtime" + m.ToString());
                    TextBox txttotalleadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txttotalleadtime" + m.ToString());

                    ddlMPNName.SelectedValue = DtMpn.Rows[m - 1]["ManufacturerID"].ToString();
                    txtMPNPartNo.Text = DtMpn.Rows[m - 1]["MPNCODE"].ToString();
                    txtMfrLeadtime.Text = DtMpn.Rows[m - 1]["MFRLEADTIME"].ToString();
                    txttransiteLeadtime.Text = DtMpn.Rows[m - 1]["TRANSITELTIME"].ToString();
                    if (txttransiteLeadtime.Text != "" && txtMfrLeadtime.Text != "")
                    {
                        txttotalleadtime.Text = (Convert.ToDouble(txttransiteLeadtime.Text) + Convert.ToDouble(txtMfrLeadtime.Text)).ToString();
                    }
                    //For Mpn Log
                    MPNLogDetail += "MPNPartNo:" + txtMPNPartNo.Text + ",MPN Name:" + ddlMPNName.SelectedItem + ",MFRLeadTime:" + txtMfrLeadtime.Text + ",TransiteLeadTime:" + txttransiteLeadtime.Text + ",TotalLeadTime:" + txttotalleadtime.Text;
                }
            }
            #endregion
            //For AlternatUOM
            #region ItemAltUOM
            if (DT.Rows[0]["ITMACTIVE"].ToString() == "True")
            {
                ChkActive.Checked = true;
            }
            else
            {
                ChkActive.Checked = false;
            }
            DataTable dt = dut.getdata("select  UOMSYMBOL ,UOMID from IVR_UOM where Primary_UOMID =" + selected_primaryunit);
            if (dt.Rows.Count > 0)
            {
                DdlAltUnit.DataSource = dt;
                DdlAltUnit.DataTextField = "UOMSYMBOL";
                DdlAltUnit.DataValueField = "UOMID";
                DdlAltUnit.DataBind();
            }
            else
            {
                if (DdlAltUnit.SelectedItem != null)
                {
                    DdlAltUnit.SelectedItem.Text = string.Empty;
                }
            }
            string sqlaltunit = "select * from IVR_ITEMALTUOM where ITMID ='" + ITMID + "'";
            DataTable dtaltunit = dut.getdata(sqlaltunit);
            if (dtaltunit.Rows.Count > 0)
            {
                string selected_DdlAltUnit = dtaltunit.Rows[0]["UOMID"].ToString();
                DdlAltUnit.SelectedValue = selected_DdlAltUnit;
            }

            ItemAlternateUOMLog += "AlternateUnitofMeasurement:" + DdlAltUnit.SelectedItem + ",ActiveItem:" + ChkActive.Checked;
            #endregion
            //For OtherItem
            #region ITEMOTHER
            string sqlbill = "select * from IVR_ITEMOTH where ITMID ='" + ITMID + "'";
            DataTable dtsqlbill = dut.getdata(sqlbill);
            if (dtsqlbill.Rows.Count > 0)
            {
                TxtDimensional.Text = Convert.ToDouble(dtsqlbill.Rows[0]["UNITSIZE"].ToString()).ToString("N2");
                TxtWeight.Text = Convert.ToDouble(dtsqlbill.Rows[0]["UNITWEIGHT"].ToString()).ToString("N2");
                DdlUnitOfweight.SelectedValue = dtsqlbill.Rows[0]["UNITWEIGHT_UOMID"].ToString();
                TxtBillRef.Text = dtsqlbill.Rows[0]["BINREF"].ToString();
                TxtBillRef.Enabled = false;
                if (dtsqlbill.Rows[0]["IMAGEPATH"].ToString() != string.Empty)
                {
                    Photo.ImageUrl = dtsqlbill.Rows[0]["IMAGEPATH"].ToString();
                    hidsecond.Value = dtsqlbill.Rows[0]["IMAGEPATH"].ToString();
                }
                else
                {
                    Photo.ImageUrl = "~/images/noimg1.png";
                }
            }
            //For log
            ItemOtherLogDetail += "BinRef:" + TxtBillRef.Text + ",Dimension:" + TxtDimensional.Text + ",Weight:" + TxtWeight.Text + ",UnitOfWeight:" + DdlUnitOfweight.SelectedItem + ",SelectedImageFile:" + Photo.ImageUrl;
            #endregion
            #region Opening Balance
            string SqlOpening = "select * from IVR_ITEMOPENBAL where ITMID='" + ITMID + "'";
            DataTable DtOpeningBal = dut.getdata(SqlOpening);
            if (DtOpeningBal.Rows.Count > 0)
            {
                if (DtOpeningBal.Rows[0]["LeasedType"].ToString() == "Consigned")
                {
                    ddlLeasedType.SelectedValue = "2";
                }
                else if (DtOpeningBal.Rows[0]["LeasedType"].ToString() == "Owned")
                {
                    ddlLeasedType.SelectedValue = "1";
                }
                ddlLeasedType.Enabled = false;
                txtqtymain.Text = DtOpeningBal.Rows[0]["QUANTITY"].ToString();
                txtratemain.Text = DtOpeningBal.Rows[0]["RATE"].ToString();
                txtvaluerateqtymain.Text = DtOpeningBal.Rows[0]["AMOUNT"].ToString();
                if (Convert.ToInt32(DtOpeningBal.Rows[0]["ISSUEQTY"].ToString()) > 0)
                {
                    txtqtymain.Enabled = false;
                    txtratemain.Enabled = false;
                }
            }
            #endregion
            //Creating log For Editing.
            string ItemDetail = "Item Select For Edition:-" + ItemMasterlog + "," + MPNLogDetail + "," + ItemOtherLogDetail + "," + ItemAlternateUOMLog + "," + OpeningBalanceLog;
            ObjLog.SetItemLogs(UserId, Username, CommonLog.Type.ActivityLog.ToString(), ItemDetail, "NewItemEdit.aspx", IP, System.DateTime.Now.ToString());
        }
    }
    #endregion
    //For the Page Function required in the Page
    #region
    protected void GetManufacturer()
    {
        string sqlstr = "select * from MAS_MANUFACTURE";
        DataTable dt = dut.getdata(sqlstr);
        int count = tblMPN.Rows.Count - 1;
        if (dt.Rows.Count > 0)
        {
            DropDownList DDlManufacturer = new DropDownList();
            if (count > 3)
            {
                DDlManufacturer = (DropDownList)tblMPN.FindControl("ddlManufacturer" + count);
                DDlManufacturer.DataSource = dt;
                DDlManufacturer.DataTextField = "manufactureName";
                DDlManufacturer.DataValueField = "ManufacID";
                DDlManufacturer.DataBind();
                DDlManufacturer.Items.Insert(0, new ListItem("-- manufacturer --", "0"));
            }
            else
            {
                for (int i = 1; i <= count; i++)
                {
                    DDlManufacturer = (DropDownList)tblMPN.FindControl("ddlManufacturer" + i.ToString());
                    DDlManufacturer.DataSource = dt;
                    DDlManufacturer.DataTextField = "manufactureName";
                    DDlManufacturer.DataValueField = "ManufacID";
                    DDlManufacturer.DataBind();
                    DDlManufacturer.Items.Insert(0, new ListItem("-- manufacturer --", "0"));
                }
            }
        }
    }
    protected void openingdate()
    {
        int count = Convert.ToInt32(hidsecond.Value);
        for (int i = 1; i <= count; i++)
        {
            TextBox txtOpeningdate = (TextBox)tbldisbatch.Rows[i - 1].FindControl("txtopeningdate");
            int year = System.DateTime.Now.Year;
            txtOpeningdate.Text = year + "-" + "04-01";
        }
    }
    protected void GetProject()
    {
        string sqlstrprj = "select * from MAS_PROJECT";
        DataTable dt = dut.getdata(sqlstrprj);
        ddlproject.DataSource = dt;
        ddlproject.DataTextField = "projectname";
        ddlproject.DataValueField = "projectid";
        ddlproject.DataBind();
        ddlproject.Items.Insert(0, new ListItem("-- Select Project Name --", "0"));
    }
    protected void BindDdlUnitOfweight()
    {
        DataTable DT = dut.getdata("select UOMSYMBOL, UOMID from IVR_UOM");

        DdlUnitOfweight.DataSource = DT;
        DdlUnitOfweight.DataTextField = "UOMSYMBOL";
        DdlUnitOfweight.DataValueField = "UOMID";
        DdlUnitOfweight.DataBind();
        DdlUnitOfweight.Items.Insert(0, new ListItem("Select Unit of Weight", "0"));
    }
    private void BindItemGroup() // bind Item
    {
        //DataTable DT = dut.getdata("select * from IVR_ITEMGROUP");
        DataTable Dt = dut.getdata("Usp_IVR_group");
        if (Dt.Rows.Count > 0)
        {
            DDLItemGroup.DataSource = Dt;
            DDLItemGroup.DataTextField = "GroupName";
            DDLItemGroup.DataValueField = "IGRID";
            DDLItemGroup.DataBind();
            DDLItemGroup.Items.Insert(0, new ListItem("-- Select Item Group --", "0"));
        }
    }
    private void BindStock()
    {
        DataTable DT = dut.getdata("select * from IVR_StockType where stockTypeid=1");
        DDLStockType.DataSource = DT;
        DDLStockType.DataTextField = "StockTypeName";
        DDLStockType.DataValueField = "StockTypeId";
        DDLStockType.DataBind();
        DDLStockType.Items.Insert(0, new ListItem("-- Select Stock Type --", "0"));
    }
    private void BindCategory()
    {
        DataTable DT = dut.getdata("select * from IVR_ITEMCATEGORY where ICAID=2");
        DDLCategory.DataSource = DT;
        DDLCategory.DataTextField = "ICANAME";
        DDLCategory.DataValueField = "ICAID";
        DDLCategory.DataBind();
    }
    private void BindPrimaryUnit()
    {
        DataTable DT = dut.getdata("select * from IVR_UOM");
        DDLPrimaryUnit.DataSource = DT;
        DDLPrimaryUnit.DataTextField = "UOMSYMBOL";
        DDLPrimaryUnit.DataValueField = "UOMID";
        DDLPrimaryUnit.DataBind();
        DDLPrimaryUnit.Items.Insert(0, new ListItem("-- Select Unit Of Measurement --", "0"));
    }
    private void GetLocation()
    {
        DataTable dtWHL = dut.getdata("select * from IVR_Warehouse");
        int count = Convert.ToInt32(hidsecond.Value);
        for (int i = 1; i <= count; i++)
        {
            DropDownList ddlloc = (DropDownList)tbldisbatch.Rows[i - 1].FindControl("ddlloc");
            ddlloc.DataSource = dtWHL;
            ddlloc.DataTextField = "WHSLOCATION";
            ddlloc.DataValueField = "WHSID";
            ddlloc.DataBind();
            //ddlloc.SelectedValue = dtWHL.Rows[i]["WHSID"].ToString();
        }
    }
    protected void Itemsettings()
    {
        string strsql = "select itmcode, itmadditionaldesc, itmaltdesc, ITMALTUNIT,  itmupccode, itmbinref from ivr_itemsettings";
        DataTable dt = dut.getdata(strsql);
        if (dt.Rows.Count > 0)
        {
            if (Convert.ToDouble(dt.Rows[0]["ITMALTUNIT"]) > 0)
            {
                DdlAltUnit.Enabled = true;
            }
            else
            {
                DdlAltUnit.Enabled = false;
                DdlAltUnit.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }

            if (Convert.ToDouble(dt.Rows[0]["itmupccode"]) > 0)
            {
                //TxtUpcCode.Enabled = true;
            }
            else
            {
                //TxtUpcCode.Enabled = false;
                //TxtUpcCode.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }

            if (Convert.ToDouble(dt.Rows[0]["itmbinref"]) > 0)
            {
                TxtBillRef.Enabled = true;
            }
            else
            {
                TxtBillRef.Enabled = false;
                TxtBillRef.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }
            if (Convert.ToDouble(dt.Rows[0]["itmcode"]) > 0)
            {
                TxtItemCode.Enabled = true;
                RFVItemCode.Enabled = true;
            }
            else
            {
                RFVItemCode.Enabled = false;
                TxtItemCode.Enabled = false;
                TxtItemCode.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }
            if (Convert.ToDouble(dt.Rows[0]["itmaltdesc"]) > 0)
            {
                TxtAlternateDesc.Enabled = true;
            }
            else
            {
                TxtAlternateDesc.Enabled = false;
                TxtAlternateDesc.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }

            if (Convert.ToDouble(dt.Rows[0]["itmadditionaldesc"]) > 0)
            {
                TxtAdditionalDesc.Enabled = true;
            }
            else
            {
                TxtAdditionalDesc.Enabled = false;
                TxtAdditionalDesc.BackColor = System.Drawing.ColorTranslator.FromHtml("#ece9d8");
            }
        }
    }
    protected void BindPrimaryAltUnit()
    {
        DataTable dt = dut.getdata("select  UOMSYMBOL ,UOMID from IVR_UOM where Primary_UOMID =" + DDLPrimaryUnit.SelectedValue.ToString());
        if (dt.Rows.Count > 0)
        {
            DdlAltUnit.DataSource = dt;
            DdlAltUnit.DataTextField = "UOMSYMBOL";
            DdlAltUnit.DataValueField = "UOMID";
            DdlAltUnit.DataBind();
        }
        else
        {
            if (DdlAltUnit.SelectedItem != null)
            {
                DdlAltUnit.SelectedItem.Text = string.Empty;
            }
        }
    }    
    #endregion
    //For the Control Change or Control Selected event
    #region
    protected void TxtItemDescription_TextChanged(object sender, EventArgs e)
    {
        DataTable dtItemdesc = dut.getdata("select ITMCODE From IVR_ITEM Where ITMDESC ='" + TxtItemDescription.Text + "'");
        if (dtItemdesc.Rows.Count > 0)
        {
            AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(Page, GetType(), "ShowAlert", "CheckItemName()", true);
        }
    }
    protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList DDL = (DropDownList)sender;
        string ddid = DDL.ID.ToString().Substring(11);
        if (DDL.SelectedValue != "0")
        {
            string item_id = DDL.SelectedValue;
            string sql = "select WHSLOCATION, WHSID from IVR_WAREHOUSE";
            DataTable DT = dut.getdata(sql);
        }
        else
        {
        }
    }
    protected void DDLPrimaryUnit_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindPrimaryAltUnit();
    }
    #endregion
    //For the Click of the Page Event
    #region
    protected void Update_Click(object sender, EventArgs e)
    {
        try
        {
            //using (TransactionScope scope = new TransactionScope())
            //{
                string IP = Request.ServerVariables["REMOTE_ADDR"];
                updatemain_Item();
                UpdateMpn();
                update_billrefnumber();
                updateIVR_ITEMALTUOM();
                update_IVROPENBAL();
                //scope.Complete();
                //Creating log For the item.
                string ItemDetail = "Item Updated with Following details:-" + UpdateItemMasterlog + "," + UpdateMPNLogDetail + "," + UpdateItemOtherLogDetail + "," + UpdateItemAlternateUOMLog + "," + UpdateOpeningBalanceLog;
                ObjLog.SetItemLogs(UserId, Username, CommonLog.Type.ActivityLog.ToString(), ItemDetail, "NewItemEdit.aspx", IP, System.DateTime.Now.ToString());
           // }
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Information", "showMsg();", true);
        }
        catch (Exception ex)
        {
            LblMsg.Text = ex.Message;
        }
    }
    protected void UpdateMpn()
    {
        string strMpn = "Select ID from IVR_ITEM_MPN where ITEMID='" + ITMID + "'";
        DataTable DTMPN = dut.getdata(strMpn);
        string ID = "";
        if (DTMPN.Rows.Count > 0)
        {
            for (int m = 1; m < tblMPN.Rows.Count; m++)
            {
                ID = DTMPN.Rows[m - 1]["ID"].ToString();
                TextBox txtMPNPartNo = (TextBox)tblMPN.Rows[m - 1].FindControl("txtmanufacturecode" + m.ToString());
                DropDownList ddlMPNName = (DropDownList)tblMPN.Rows[m - 1].FindControl("ddlManufacturer" + m.ToString());
                TextBox txtMfrLeadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txtmfrleadtime" + m.ToString());
                TextBox txttransiteLeadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txttransitleadtime" + m.ToString());
                TextBox txttotalleadtime = (TextBox)tblMPN.Rows[m - 1].FindControl("txttotalleadtime" + m.ToString());
                if (txttotalleadtime.Text == "")
                {
                    if (txtMfrLeadtime.Text != "" && txttransiteLeadtime.Text != "")
                    {
                        txttotalleadtime.Text = (Convert.ToDecimal(txtMfrLeadtime.Text) + Convert.ToDecimal(txttransiteLeadtime.Text)).ToString();
                    }
                }
                SqlParameter[] MPNparam = {
                                                    new SqlParameter("@ID",ID),
                                                    new SqlParameter("@MPNCODE",txtMPNPartNo.Text),
                                                    new SqlParameter("@ManufactureID",ddlMPNName.SelectedValue),
                                                    new SqlParameter("@MFRLEADTIME",txtMfrLeadtime.Text),
                                                    new SqlParameter("@TRANSITELTIME",txttransiteLeadtime.Text),
                                                    new SqlParameter("@TOTALLEADTIME",txttotalleadtime.Text),
                                                    new SqlParameter("@ename", Session["username"].ToString()) 
                                                  };
                int itempn = dut.executeprocedure("USP_IVR_UPDATE_MPN", MPNparam);

                //For Update log
                UpdateMPNLogDetail += "Item ID:" + ID + ",MPNPartNo:" + txtMPNPartNo.Text + ",MPN Name:" + ddlMPNName.SelectedItem + ",MFRLeadTime:" + txtMfrLeadtime.Text + ",TransiteLeadTime:" + txttransiteLeadtime.Text + ",TotalLeadTime:" + txttotalleadtime.Text;
            }
        }
    }
    protected void updatemain_Item()
    {
        try
        {
            string itemBatchOpt = "0";
            string item_code = TxtItemCode.Text;
            
            int active = 0;
            if (ChkActive.Checked == true)
            {
                active = 1;
            }
            double op_stock = 0;
            if (hidTotalOp.Value != "")
            {
                op_stock = Convert.ToDouble(hidTotalOp.Value.Trim());
            }
            SqlParameter[] parm ={
                                      new SqlParameter("@ITMID",ITMID),
                                      new SqlParameter("@ITMCODE", TxtItemCode.Text.Trim()),
                                      new SqlParameter("@ITMDESC", TxtItemDescription.Text.Trim()),
                                      new SqlParameter("@ITMACTIVE", active),
                                      new SqlParameter("@ITMALTDESC", TxtAlternateDesc.Text.Trim()),
                                      new SqlParameter("@ITMADDDESC", TxtAdditionalDesc.Text.Trim()),
                                      new SqlParameter("@IGRID", DDLItemGroup.SelectedValue),
                                      new SqlParameter("@ICAID",DDLCategory.SelectedValue),                                                                           
                                      new SqlParameter("@StockTypeId", DDLStockType.SelectedValue),                                      
                                      new SqlParameter("@ITMBATCH", false),
                                      //new SqlParameter("@UPCSKUHSNCODE", TxtUpcCode.Text.Trim()),
                                      new SqlParameter("@LocationId",txtlocationid.Text),
                                      new SqlParameter("@ITMBATCHOPT",itemBatchOpt),
                                      new SqlParameter("@PROJECTID",ddlproject.SelectedValue),
                                      new SqlParameter("@PRIMARY_UOMID",DDLPrimaryUnit.SelectedValue),
                                      new SqlParameter("@MAXQTY",Convert.ToDouble(TxtMaximum.Text.Trim())),
                                      new SqlParameter("@MINQTY",Convert.ToDouble(TxtMinimum.Text.Trim())),
                                      new SqlParameter("@REORDERQTY",TxtReorder.Text.Trim()),
                                      new SqlParameter("@OpeningStock", op_stock)  ,
                                      new SqlParameter("@EName",Session["username"].ToString())                                      
                                   };
            int result = dut.executeprocedure("usp_update_IVR_Item", parm);
            //For Update log
            UpdateItemMasterlog = "Item Code:" + TxtItemCode.Text + ",ItemDesc:" + TxtItemDescription.Text +
                ",AlternateDesc:" + TxtAlternateDesc.Text + ",AdditionalDesc:" + TxtAdditionalDesc.Text +
                ",ItemGroup:" + DDLItemGroup.SelectedItem + ",ItemCategory:" + DDLCategory.SelectedItem +
                ",StockType:" + DDLStockType.SelectedItem + ",PrimaryUnit:" + DDLPrimaryUnit.SelectedItem + ",Maximum:" + TxtMaximum.Text +
                ",Minimum:" + TxtMinimum.Text + ",Reorder:" + TxtReorder.Text + ",OpeningStock" + op_stock + ",Project:" + ddlproject.SelectedItem;
            //}        
        }
        catch (Exception ex)
        {
            LblMsg.Attributes.Remove("class");
            LblMsg.Attributes.Add("class", "false");
            LblMsg.Text = ex.Message;
            LblMsg.Text += ex.Source;
        }
    }
    protected void update_billrefnumber()
    {
        string fulpath = "";
        string oldpath = hidsecond.Value;
        if (fld.HasFile)
        {
            string unique_name = Guid.NewGuid().ToString().Replace("-", string.Empty);
            FileInfo finfo = new FileInfo(fld.PostedFile.FileName);
            string ext = finfo.Extension;
            fulpath = "~/Main/Inventory/item/image/" + unique_name + ext;
            fld.SaveAs(Server.MapPath(fulpath));
            if (oldpath != string.Empty)
            {
                File.Delete(Server.MapPath(oldpath));
            }
        }
        else
        {
            fulpath = oldpath;
        }
        // INSERT BILL REF NUMBER FROM TABLE IVR_ITEMOTH  //
        SqlParameter[] PARMBILL = { new SqlParameter("@BINREF",TxtBillRef.Text.Trim()),
                                            new SqlParameter("@ITMID", ITMID),
                                            new SqlParameter("@UNITSIZE", TxtDimensional.Text.Trim()),
                                            new SqlParameter("@UNITWEIGHT",Convert.ToDouble(TxtWeight.Text.Trim())),
                                            new SqlParameter("@UNITWEIGHT_UOMID", DdlUnitOfweight.SelectedValue),
                                            new SqlParameter("@IMAGEPATH", fulpath.ToString()),
                                            new SqlParameter("@EName", Session["username"].ToString()) };
        int bin= dut.executeprocedure("usp_ivr_update_ITEMOTH", PARMBILL);
        //For Update log
        UpdateItemOtherLogDetail += "ItemId:" + ITMID + ",BinRef:" + TxtBillRef.Text + ",Dimension:" + TxtDimensional.Text + ",Weight:" + TxtWeight.Text + ",UnitOfWeight:" + DdlUnitOfweight.SelectedItem + ",SelectedImageFile:" + fulpath.ToString();
    }
    protected void updateIVR_ITEMALTUOM()
    {
        //INSERT ALTUNIT FROM TABLE IVR_ITEMALTUOM //    
        SqlParameter[] parmaltunit = { new SqlParameter("@UOMID", DdlAltUnit.SelectedValue),
                                                 new SqlParameter("@ITMID", ITMID),
                                                 new SqlParameter("@EName", Session["username"].ToString())
                                             };
        int alt=dut.executeprocedure("usp_ivr_update_ITEMALTUOM", parmaltunit);
        //For Log
        UpdateItemAlternateUOMLog += "ItemID:" + ITMID + ",AlternateUnitofMeasurement:" + DdlAltUnit.SelectedItem;
    }
    protected void update_IVROPENBAL()
    {
        if (txtvaluerateqtymain.Text == "")
        {
            txtvaluerateqtymain.Text = Convert.ToString(Convert.ToDouble(txtqtymain.Text) * Convert.ToDouble(txtratemain.Text));
        }
        SqlParameter[] parm2 = {
                                                new SqlParameter("@ITMID",ITMID),                                                
                                                new SqlParameter("@WHSID",ddlloc.SelectedValue),
                                                new SqlParameter("@LeasedType",ddlLeasedType.SelectedItem.Text),
                                                new SqlParameter("@QUANTITY",Convert.ToDouble(txtqtymain.Text.Trim())),
                                                new SqlParameter("@RATE",Convert.ToDouble(txtratemain.Text.Trim())),
                                                new SqlParameter("@AMOUNT",Convert.ToDouble(txtvaluerateqtymain.Text).ToString())                                                                                                         
                                           };
        int insert = dut.executeprocedure("USP_IVR_Update_ItemOpeningBalance", parm2);

        //For Update Log
        UpdateItemOtherLogDetail += "ItemID:" + ITMID + ",Location:" + ddlloc.SelectedItem + ",Qty:" + txtqtymain.Text + ",Rate:" + txtratemain.Text + ",LeasedType:" + ddlLeasedType.SelectedItem + ",Value:" + txtvaluerateqtymain.Text;
    }
    protected void Close_Click(object sender, EventArgs e)
    {
        Response.Redirect("Item.aspx?action=view");
    }
    #endregion
    
}
